using System.IO;
using System.Net;

namespace OpenWaves.ImageTransformations.Web
{
    public class WebFileProvider : IVirtualFileProvider
    {
        public IVirtualFile GetFile(Url fileUrl)
        {
            var url = fileUrl as AbsoluteUrl;
            if (url == null)
                return null;

            return new WebFile(url);
        }

        private class WebFile : IVirtualFile
        {
            private readonly AbsoluteUrl url;

            public WebFile(AbsoluteUrl url)
            {
                this.url = url;
            }

            public string Hash
            {
                get { return null; }
            }

            public Url Url
            {
                get { return this.url; }
            }

            public Stream Open()
            {
                var webRequest = (HttpWebRequest)WebRequest.Create(this.url);
                webRequest.Method = "GET";
                return webRequest.GetResponse().GetResponseStream();
            }

        }
    }
}